package com.qingmi.farmer.ui.order.grain;


import android.view.View;

import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.UserData;
import com.qingmi.farmer.data.model.GrainOrder;
import com.qingmi.farmer.data.repository.data.GrainDataRepository;
import com.qingmi.farmer.databinding.FragmentGrainOrderCompletedBinding;
import com.qingmi.farmer.event.GrainOrderDataChangeEvent;
import com.qingmi.farmer.event.UserLoginChangeEvent;
import com.wjjun.base.ui.base.ListFragment;
import com.qingmi.farmer.viewmodel.GrainOrderItemViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 卖出订单-已完成
 */
public class GrainOrderCompletedFragment extends ListFragment<GrainOrderCompletedViewModel, FragmentGrainOrderCompletedBinding> {


    @Inject
    GrainDataRepository grainDataRepository;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_grain_order_completed;
    }

    @Override
    protected GrainOrderCompletedViewModel initViewModel() {
        return new GrainOrderCompletedViewModel();
    }

    @Override
    protected void initContent() {
        grainDataRepository.bind(this);
        super.initContent();
        commListViewModel.setEnableLoadMore(false);
        commListViewModel.setEmpty(getString(R.string.prompt_no_order), R.drawable.ic_no_order);
        RxBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataChange(GrainOrderDataChangeEvent event) {
        if (event.getStatus() == DataSetting.GRAIN_ORDER_STATUS_COMPLETED
                || event.getStatus() == DataSetting.GRAIN_ORDER_STATUS_CANCEL){
            notifyDataChange();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserLoginChange(UserLoginChangeEvent event) {
        notifyDataChange();
    }


    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        if (item instanceof GrainOrderItemViewModel) {
            itemBinding.set(BR.viewModel, R.layout.layout_item_grain_order_completed)
                    .bindExtra(BR.presenter, viewGroupPresenter);
        }
    }

    @Override
    protected void onListItemClick(View view, BaseViewModel item) {
        super.onListItemClick(view, item);
        if (getActivity() == null) {
            showToast(R.string.error_activity_is_null);
            return;
        }
        if (item instanceof GrainOrderItemViewModel) {
            switch (view.getId()) {
                case R.id.vg_item://item
                    GrainOrderDetailActivity.start(getActivity(), ((GrainOrderItemViewModel) item).id);
                    break;
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void refreshData(boolean init) {
        if (!UserData.getInstance().isLogin()) {
            showLogin();
            return;
        }
        super.refreshData(init);
        grainDataRepository.getGrainCompletedList(new DefaultObserver<List<GrainOrder>>() {
            @Override
            public void onNext(List<GrainOrder> grainOrders) {
                List<GrainOrderItemViewModel> list = new ArrayList<>();
                if (grainOrders != null && !grainOrders.isEmpty()) {
                    for (GrainOrder item : grainOrders) {
                        list.add(new GrainOrderItemViewModel(item));
                    }
                }

                resetData(list);
            }

            @Override
            public void onError(MyException e) {
                finisRefresh();
                commListViewModel.setError(e.getMessage());
                commListViewModel.showError();
            }
        });
    }


}
